<template>
  <div class="ICSearch">
    <div class="ic-con">
      <div class="home-title">
        <p class="t-title">智慧产业创新数据库</p>
        <div class="t-search">
          <div class="flex">
            <!-- <el-autocomplete
              ref="autocomplete"
              popper-class="elAuto"
              class="elinp"
              v-model="searchTxt"
              :trigger-on-focus="false"
              placeholder="请输入想查询的产业技术节点名称"
              suffix-icon="el-icon-search"
              :fetch-suggestions="querySearchAsync"
              @select="handleSearch"
            >
              <template slot-scope="props">
                <div class="wrapper">
                  <p class="name">{{ props.item.name }}</p>
                  <p class="addr">{{ props.item.chain_name }}</p>
                </div>
              </template>
            </el-autocomplete> -->
            <el-select
              ref="autocomplete"
              popper-class="elAuto"
              class="elinp"
              v-model="searchTxt"
              placeholder="请输入想查询的产业技术节点名称"
              filterable
              remote
              :remote-method="remoteMethod"
              @change="handleSearch"
            >
              <!-- <template slot="prefix">
                <span style="padding-left: 5px;">
                  <i class="el-icon-search"></i>
                </span>
              </template> -->
              <el-option-group
                v-for="(group, index) in options3"
                :key="index"
                :label="group.label"
              >
                <el-option
                  v-for="item in group.options"
                  :key="item.eid"
                  :label="item.name"
                  :value="item"
                >
                  <div class="wrapper">
                    <p class="name">{{ item.name }}</p>
                    <p class="addr">{{ item.chain_name }}</p>
                  </div>
                </el-option>
              </el-option-group>
            </el-select>
          </div>
          <p class="search-tip">
            热门产业节点：<span
              v-for="(item, idx) in searchList"
              :key="idx.dictValue"
              @click="handleClick(item.dictValue)"
              >{{ item.dictValue }}</span
            >
          </p>
        </div>
      </div>
      <div class="countbar-wrap">
        <div>
          <p class="counted">{{ bannerData.chainSize }}</p>
          <p>核心产业</p>
        </div>
        <p class="line"></p>
        <div>
          <p class="counted">{{ bannerData.nodeSize }}</p>
          <p>产业技术点</p>
        </div>
        <p class="line"></p>
        <div>
          <p class="counted">{{ bannerData.companySize }}</p>
          <p>产业机构</p>
        </div>
        <p class="line"></p>
        <div>
          <p class="counted">{{ bannerData.expertSize }}</p>
          <p>领军专家</p>
        </div>
      </div>
    </div>
    <div class="flex c-con">
      <div
        class="chain"
        v-for="list in chainList"
        :key="list.type"
        @click="toDetail(list)"
      >
        <img :src="list.imgUrl" alt="" />
        <p class="txt">{{ list.dictValue }}</p>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      searchTxt: "",
      searchList: [],
      bannerData: {
        chainSize: 0,
        nodeSize: 0,
        companySize: 0,
        expertSize: 0,
      },
      options3: [
        {
          label: "相关推荐",
          options: [],
        },
        // {
        //   label: "相关推荐",
        //   options: [],
        // },
      ],
      chainList: [],
      timeout: null,
    };
  },
  created() {
    this.getDictAll();
    this.getTotal();
  },
  methods: {
    // 获取产业链全景图字典数据
    getDictAll() {
      const data = JSON.parse(localStorage.getItem("allDict"));
      const industryChain = data.find((list) => {
        return list.dictType == "industry_chain";
      }).dataList;
      // localStorage.setItem("industryChain", JSON.stringify(industryChain));
      industryChain.forEach((chain, idx) => {
        chain.imgUrl = require(`@/assets/img/IC/${idx + 1}.jpeg`);
      });
      this.chainList = industryChain;
      this.searchList = data.find((list) => {
        return list.dictType == "hot_industry";
      }).dataList;
    },
    getTotal() {
      this.$get(this.$Url.chain.chainCount, {}).then((res) => {
        // const data = res.data;
        // console.log("data", data);
        this.bannerData = res.data;
      });
    },
    remoteMethod(queryString) {
      // console.log("queryString", queryString);
      // return;
      clearTimeout(this.timeout);
      this.timeout = setTimeout(() => {
        this.$get(this.$Url.chain.queryNodesByKeyword, {
          keyword: queryString,
        }).then((res) => {
          const data = res.data;
          // console.log("data", data);
          let cyl = [],
            jsl = [];
          data.forEach((item) => {
            if (item.chain_type == "CYL") {
              cyl.push(item);
            } else if (item.chain_type == "JSL") {
              jsl.push(item);
            }
          });
          this.options3[0].options = jsl;
          // this.options3[0].options = cyl;
          // this.options3[1].options = jsl;
        });
      }, 300);
    },
    // 远程搜索
    querySearchAsync(queryString, cb) {
      clearTimeout(this.timeout);
      this.timeout = setTimeout(() => {
        this.$get(this.$Url.chain.queryNodesByKeyword, {
          keyword: queryString,
        }).then((res) => {
          const data = res.data;
          // console.log("data", data);
          cb(data);
        });
        // const result = [{ value: "光伏产业链全景图" }];
        // cb(result);
      }, 300);
    },
    // 选中节点，然后跳转
    handleSearch(item) {
      // console.log(item);
      const list = {
        dictLabel: item.chain_name,
        eid: item.eid,
        name: item.name,
      };
      this.toDetail(list);
    },
    // 热门产业节点--触发弹窗querySearchAsync
    handleClick(val) {
      // this.$refs.autocomplete.activated = true;
      // this.searchTxt = val;
      // this.$refs.autocomplete.getData(val);
      // this.searchTxt = val;
      this.$refs.autocomplete.focus();
      this.remoteMethod(val);
    },
    // 详情
    toDetail(list) {
      // if (list.dictLabel != "光伏产业链全景图") {
      //   this.$message({
      //     type: "warning",
      //     message: "开发中，敬请等待！",
      //   });
      //   return;
      // }
      this.$router.push({
        name: "ICDetail",
        query: {
          dictLabel: list.dictLabel,
          eid: list.eid || "",
          name: list.name || "",
        },
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.ICSearch {
  background-color: rgba(153, 204, 255, 0.16);
  padding-top: 40px;
  .ic-con {
    width: 80%;
    margin: 0 auto;
  }
  .t-title {
    color: $mainColor;
    font-size: 36px;
    text-align: center;
    font-weight: bold;
    margin-bottom: 40px;
  }
  .t-search {
    padding: 0 200px;
  }
  .elinp {
    width: 100%;
    ::v-deep {
      .el-input__inner {
        // width: 100%;
        height: 0.54rem;
        line-height: 0.54rem;
        border-color: $mainColor;
        border-radius: 0.27rem;
        font-size: 0.18rem;
        color: #283146;
        padding-right: 60px;
        &:focus {
          border-color: $mainColor;
        }
      }
      .el-input__suffix {
        right: 20px;
      }
      .el-icon-search:before {
        display: inline-block;
        font-size: 20px;
        margin-top: 3px;
        color: $mainColor;
      }
    }
  }
  .search-button {
    width: 0.94rem;
    height: 0.54rem;
    border-radius: 0.08rem;
    font-size: 0.18rem;
    color: #fff;
    background-color: $mainColor;
    margin-left: 0.14rem;
  }
  .search-tip {
    margin-top: 15px;

    span {
      margin-right: 15px;
      color: $mainColor;
      cursor: pointer;
    }
  }
  .countbar-wrap {
    // height: 100px;
    padding: 30px 50px;
    display: flex;
    justify-content: space-around;
    font-weight: bold;

    div {
      text-align: center;
      min-width: 200px;

      .counted {
        font-size: 22px;
        margin-bottom: 15px;
      }
    }
    .line {
      border-left: 2px solid #999;
    }
  }
  .c-con {
    padding-bottom: 30px;
    background-color: #fff;
    flex-wrap: wrap;
    justify-content: space-around;

    .chain {
      width: 32%;
      text-align: center;
      margin-top: 30px;
      cursor: pointer;
      position: relative;
      img {
        width: 100%;
        z-index: -1;
      }
      .txt {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        width: 140px;
        font-size: 24px;
        z-index: 10;
        color: #fff;
      }
    }
  }
}
.elAuto {
  .wrapper {
    display: flex;
  }
  .name {
    text-overflow: ellipsis;
    overflow: hidden;
    flex: 1;
  }
  .addr {
    width: 100px;
    font-size: 12px;
    color: #b4b4b4;
  }
}
</style>
